package com.ruoyi.wwc.user.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wwc.user.domain.User;
import com.ruoyi.wwc.user.service.IUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户信息Controller
 *
 * @author wwc
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("user:user:list")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 查询用户信息列表
     */
    @InnerAuth
    @GetMapping("/getList")
    public TableDataInfo getList(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据phone查询用户信息列表
     */
//    @RequiresPermissions("user:user:list")
    @GetMapping("/getUser")
    public R<?> getUser(User user)
    {
        User user1 = userService.selectUserByPhone(user.getPhone());
        if (user1 == null){
            return R.fail("没有对应的用户信息！");
        }
        return R.ok(user1);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("user:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @RequiresPermissions("user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 新增用户信息
     */
//    @RequiresPermissions("user:user:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        SysUser sysUser = new SysUser();

        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
        if (user.getUserName() == null){
            user.setUserName("用户"+uuid);
        }
        if (user.getNickName() == null){
            user.setNickName("用户"+uuid);
        }
        if (user.getPassword() == null){
            user.setPassword("123456");
        }
        sysUser.setUserName(user.getUserName());
        sysUser.setPhonenumber(user.getPhone());
        sysUser.setEmail(user.getEmail());
        sysUser.setNickName(user.getNickName());
        sysUser.setPassword(user.getPassword());
        //long数组
        Long[] roleIds = {101L};
        sysUser.setRoleIds(roleIds);//顾客角色组
//        sysUser.setRoleId(101L);
        SysUser sysUser1 = remoteUserService.addSysUser(sysUser, SecurityConstants.INNER);
        user.setSysId(sysUser1.getUserId());

        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("user:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("user:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
